package com.hwz.controller.admin;

import com.hwz.dto.DishDTO;
import com.hwz.dto.DishPageQueryDTO;
import com.hwz.entity.Dish;
import com.hwz.result.PageResult;
import com.hwz.result.Result;
import com.hwz.service.DishService;
import com.hwz.vo.DishVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * [一句话描述该类的功能]
 *
 * @author : [Lenovo]
 * @version : [v1.0]
 * @createTime : [2024/3/13 20:30]
 */
@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品管理")
public class DishController {
    @Autowired
    DishService dishService;

    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @CacheEvict("/delAll")
    @CachePut(value = "dish",key = "#dishDTO.id")
    public Result addDish(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        dishService.addDish(dishDTO);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品:{}", id);
        return Result.success(dishService.getById(id));
    }

    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    public Result<List<Dish>> getByCategoryId(Long categoryId){
        log.info("根据分类id查询菜品{}",categoryId);
        return Result.success(dishService.getByCategoryId(categoryId));
    }

    /**
     * 菜品分页
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页:{}",dishPageQueryDTO);
        PageResult pageResult = dishService.page(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 更新菜品
     * @param dishDTO
     * @return
     */
    @PutMapping
    @CacheEvict("/delAll")
    public Result updateDish(@RequestBody DishDTO dishDTO){
        log.info("更新菜品:{}",dishDTO);
        dishService.updateDish(dishDTO);
        return Result.success();
    }

    @DeleteMapping
    @CacheEvict("/delAll")
    public Result deleteDish(@RequestParam List<Long> ids){
        log.info("删除菜品:{}",ids);
        dishService.deleteDish(ids);
        return Result.success();
    }

    @PostMapping("/status/{status}")
    @CacheEvict("/delAll")
    public Result startOrOff(Long id,@PathVariable Integer status)
    {
        log.info("id:{}",id);
        log.info("status:{}",status);
        dishService.startOrOff(id,status);
        return Result.success();
    }
}
